package com.hpin.fansea.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.MyOrderResult;
import com.hpin.fansea.utils.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

/**
 * 我的订单详情页面
 * Created by Aaron on 15/9/22.
 */
public class OrderDetailActivity extends BaseActivity {

    private MyOrderResult.OrderDetail orderDetail;

    private TextView order_pay_status;
    private TextView order_ordCash;
    // private TextView order_freight;
    private TextView order_consignee;
    private TextView order_address;
    private TextView order_phone;
    // private TextView order_freight2;
    private TextView order_payCash;
//    private TextView order_redPacketCash;
    private TextView order_shopName;
    private TextView order_shopState;
    private ImageView order_image;
    private TextView order_title;
    private TextView order_totalPrice;
    private TextView order_totalNum;
    private TextView order_color;

    private TextView tv_buy_time;
    private TextView tv_right;

    private DisplayImageOptions options;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order_detail);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_loading_default_list)
                .showImageForEmptyUri(R.mipmap.icon_fail_default_list)
                .showImageOnFail(R.mipmap.icon_fail_default_list)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

        orderDetail = (MyOrderResult.OrderDetail) getIntent().getSerializableExtra("orderDetail");

        initWidget();

        setOrderData();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.order_detail));
        tv_right.setText(getResources().getString(R.string.sale_return));

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

//        // 退货申请
//        order_return_goods = (Button) findViewById(R.id.order_return_goods);
        // 支付状态
        order_pay_status = (TextView) findViewById(R.id.order_pay_status);
        // 总金额
        order_ordCash = (TextView) findViewById(R.id.order_ordCash);
        // 运费
        // order_freight = (TextView) findViewById(R.id.order_freight);
        // 收货人
        order_consignee = (TextView) findViewById(R.id.order_consignee);
        // 地址
        order_address = (TextView) findViewById(R.id.order_address);
        // 手机
        order_phone = (TextView) findViewById(R.id.order_phone);
        // 运费2
        // order_freight2 = (TextView) findViewById(R.id.order_freight2);
        // 支付金额
        order_payCash = (TextView) findViewById(R.id.order_payCash);
//        // 使用红包金额
//        order_redPacketCash = (TextView) findViewById(R.id.order_redPacketCash);
        // 商品名称
        order_shopName = (TextView) findViewById(R.id.order_shopName);
        // 商品状态 需隐藏
        order_shopState = (TextView) findViewById(R.id.order_shopState);
        order_shopState.setVisibility(View.GONE);
        // 商品图片
        order_image = (ImageView) findViewById(R.id.order_image);
        // 商品标题
        order_title = (TextView) findViewById(R.id.order_title);
        // 商品总价
        order_totalPrice = (TextView) findViewById(R.id.order_totalPrice);
        // 商品总数量
        order_totalNum = (TextView) findViewById(R.id.order_totalNum);
        // 购买时间
        tv_buy_time = (TextView) findViewById(R.id.tv_buy_time);

    }

    /**
     * 设置订单数据
     */
    private void setOrderData() {

        if (orderDetail != null) {

            //是否可以退货
            switch (orderDetail.ordStat) {

                case Constant.ORDER_STATE_CANEL:// 无法退货的状态 ----已取消

                    order_pay_status.setText("已取消");
                    tv_right.setVisibility(View.INVISIBLE);

                    break;
                case Constant.ORDER_STATE_RETURN_WAIT:// 无法退货的状态 ----退款待审核

                    order_pay_status.setText("退款待审核");
                    tv_right.setVisibility(View.INVISIBLE);

                    break;
                case Constant.ORDER_STATE_SUBMIT_REFUND_SUCCESS:// 无法退货的状态 ---- 待退款

                    order_pay_status.setText("待退款");
                    tv_right.setVisibility(View.INVISIBLE);

                    break;
                case Constant.ORDER_STATE_RETURN_REFUND_SUCCESS:// 无法退货的状态 ----已退款

                    order_pay_status.setText("已退款");
                    tv_right.setVisibility(View.INVISIBLE);
                    break;

                case Constant.ORDER_STATE_NOT_PAY:

                    order_pay_status.setText("未付款");
                    break;

                case Constant.ORDER_STATE_PAY:

                    order_pay_status.setText("已付款");
                    break;

                case Constant.ORDER_STATE_VALID:

                    order_pay_status.setText("有效订单");
                    break;

                case Constant.ORDER_STATE_ARRIVE:

                    order_pay_status.setText("到货订单");
                    break;

                case Constant.ORDER_STATE_FINISH:

                    order_pay_status.setText("已完成");
                    break;

                case Constant.ORDER_STATE_SUBMIT_REFUND_FAIL:

                    order_pay_status.setText("审核不通过");
                    break;

            }

            order_shopName.setText("订单号:" + orderDetail.ordNo);

            // 购买时间
            tv_buy_time.setText(orderDetail.createTime2);


            MyOrderResult.OrderDetail.GroupBuy groupBuy = orderDetail.groupBuy;

            if (groupBuy != null) {

                order_title.setText(groupBuy.name);// shop 标题
                ImageLoader.getInstance().displayImage(groupBuy.fmpica, order_image, options);// shop 图片

            }

            MyOrderResult.OrderDetail.OrderDelv orderDelv = orderDetail.orderDelv;

            if (orderDelv != null) {

                order_address.setText(orderDelv.addr4);// shop 地址
                order_totalPrice.setText("￥" + orderDetail.ordCash);// shop 价格 //订单金额
                order_ordCash.setText("￥" + orderDetail.ordCash);//订单金额
                order_payCash.setText("￥" + orderDetail.payCash);// shop //实际支付金额
//                order_redPacketCash.setText("￥" + orderDetail.getRedPacketCash());// shop 红包
                order_consignee.setText(orderDetail.memName);// shop 收货人
                order_phone.setText(orderDetail.mobNo);// shop 手机号
                order_totalNum.setText("X" + orderDetail.groupBuyNum);// shop 数量

            }

        }

    }


    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();
                break;

            case R.id.tv_right://申请退货

                Intent intent = new Intent(this, SaleReturnActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("orderDetail", orderDetail);
                intent.putExtras(bundle);
                startActivity(intent);

                break;

        }
    }

}
